package com.thjk.bdf.preloan.service.domain.domainservice;

import com.thjk.bdf.common.enmus.AdmissionEnum;
import com.thjk.bdf.common.utils.DateUtils;
import com.thjk.bdf.preloan.service.ext.common.ExtServiceException;
import com.thjk.bdf.preloan.service.ext.unitygateway.api.InvoiceData;
import com.thjk.bdf.preloan.service.ext.unitygateway.service.InvoiceDataQueryExtService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
/**
 * 
 * @创建日期： 2019年11月6日
 * @author 束凯
 * @Description：发票流水准入
 */
@Service
public class InvoiceAdmittanceService {

    private static final Logger logger = LoggerFactory.getLogger(InvoiceAdmittanceService.class);

    @Autowired
    private InvoiceDataQueryExtService invoiceDataQueryExtService;
    /**
     * 最早开票时间标准
     */
    @Value("${sj.flow.first.invoice.date.months}")
    private Integer firstInvoiceDateCheck;
    /**
     * 12个月发票统计额标准
     */
    @Value("${sj.flow.sum12M.invoice.total}")
    private BigDecimal invoiceTotalSumL12mCheck;

    /**
     * @param creditCode
     * @return 准入结果枚举
     * @throws ExtServiceException 
     */
    public InvoiceData inspectInvoice(String creditCode) throws ExtServiceException {
        logger.info("开始核查企业发票数据, creditCode:{}", creditCode);

        // 1 获取发票流水数据
        InvoiceData invoiceData = invoiceDataQueryExtService.queryInvoiceData(creditCode);
        if(null==invoiceData){
        	invoiceData = new InvoiceData();
        }
        // 2 校验发票流水数据
        AdmissionEnum result = verifyInvoiceData(invoiceData);
        invoiceData.setAdmissionEnum(result);

        logger.info("结束核查企业发票数据, creditCode:{}, result:{}", creditCode, result);
        return invoiceData;
    }


    /**
     * 校验企业发票数据
     *
     * @param invoiceData
     * @return
     */
    private AdmissionEnum verifyInvoiceData(InvoiceData invoiceData) {
        if (null == invoiceData) {
            return AdmissionEnum.Error_1003_1;
        }
        if (!invoiceData.getInitialGetFlag()) {
            return AdmissionEnum.Error_1003_2;
        }
        // 校验最早开票时间
        String firstInvoicedate = invoiceData.getFirstInvoicedate();

        int months = DateUtils.getMonthsBt(DateUtils.parseDate(firstInvoicedate, "yyyy-MM-dd HH:mm:ss"),
                DateUtils.getSysDate());  //月减
        if (months < firstInvoiceDateCheck)
            return AdmissionEnum.Error_1501;
        BigDecimal invoiceTotalSumL12m = invoiceData.getInvoiceTotalSumL12m();
        if(invoiceTotalSumL12m==null||invoiceTotalSumL12m.compareTo(invoiceTotalSumL12mCheck)<0){
        	return AdmissionEnum.Error_1506;
        }

//        // 校验近2个月开票金额都为0
//        List<BigDecimal> invoiceAmtSumL12m = invoiceData.getInvoiceAmtSumL12m();// 最近十四个月开票金额
//        if (null == invoiceAmtSumL12m) {
//            return AdmissionEnum.Error_1506;
//        }
//        if (invoiceAmtSumL12m.size() != 14) {
//            return AdmissionEnum.Error_1506;
//        }
//        // 最近两个月数据等于0
//        if (BigDecimal.ZERO.equals(invoiceAmtSumL12m.get(13))
//                && BigDecimal.ZERO.equals(invoiceAmtSumL12m.get(12))) {
//            return AdmissionEnum.Error_1502;
//        }
//        // 最近14个月内，开票金额为0的月份超过2次（不含本月）
//        int count = 0;
//        for (int i = 0; i < invoiceAmtSumL12m.size(); i++) {
//            if (BigDecimal.ZERO.compareTo((invoiceAmtSumL12m.get(i))) == 0) {
//                count++;
//            }
//        }
//        if (count > 2)
//            return AdmissionEnum.Error_1503;
//
//        // 近12个月流水下降是否超过30%
//        BigDecimal invoiceTotalSumRatio12m = invoiceData.getInvoiceTotalSumRatio12m();
//        if (null == invoiceTotalSumRatio12m) {
//            return AdmissionEnum.Error_1506;
//        }
//        invoiceTotalSumRatio12m = new BigDecimal(100).subtract(
//                invoiceTotalSumRatio12m.multiply(new BigDecimal(100)));
//        if (invoiceTotalSumRatio12m.compareTo(new BigDecimal(30)) > 0)
//            return AdmissionEnum.Error_1504;
//        // 近6个月流水下降是否超过40%
//        BigDecimal invoiceTotalSumRatio6m = invoiceData.getInvoiceTotalSumRatio6m();
//        if (null == invoiceTotalSumRatio6m) {
//            return AdmissionEnum.Error_1506;
//        }
//        invoiceTotalSumRatio6m = new BigDecimal(100).subtract(
//                invoiceTotalSumRatio6m.multiply(new BigDecimal(100)));
//        if (invoiceTotalSumRatio6m.compareTo(new BigDecimal(40)) > 0)
//            return AdmissionEnum.Error_1505;

        return AdmissionEnum.Success_000000;
    }

}
